//
//  Player.cpp
//  Space shooting
//
//  Created by Raiden on 9/10/13.
//
//

#include "Player.h"
#include "SimpleAudioEngine.h"
#include "Game.h"
#include "Constants.h"
#include "Bullet01.h"

using namespace cocos2d;
using namespace std;

Player::Player() {
    
    // Init player status
    bulletType = 1;
    maxHp = hp = 10;
    isDead = false;
    orbNumber = 0;
    
    size = CCDirector::sharedDirector()->getWinSize();
    
    // Fire bullet
    schedule(schedule_selector(Player::startFire), 0.2);
    
    explotion = CCMWSprite::create("explotion01.anu", 0, "explotion01.png", NULL);
    
    scheduleUpdate();
}

Player::~Player(){
    
    removeAllChildrenWithCleanup(true);
}

Player* Player::create() {
    
    Player *player = new Player;
    
    if(player && player->initWithFile("ship_01.png")) {
        
        player->autorelease();
        
        return player;
    }
    
    CC_SAFE_DELETE(player);
    return NULL;
}

void Player::update(float delta) {
    
    // Update fire type base on orb number
    
    if(orbNumber >= 20 && orbNumber <= 40) {
        bulletType = 2;
    } else if(orbNumber >= 40) {
        bulletType = 3;
    }
    
    // Check hp status
    if(hp <= 0 && !isDead) {
        selfExplotion();
    }
    
    // Finish explotio
    if(explotion->isAnimationEnded()) {
        
        // Remove explotion from parent
        getParent()->removeChild(explotion);
        
        // Remove itself from parent
        removeFromParentAndCleanup(true);
    }
}

void Player::selfExplotion() {   
    
    isDead = true;
    GameStatus = GAME_STATUS_LOSE;
    
    // Set base on player explotion
    explotion->setPosition(this->getPosition());
    
    // Add explotion to parent
    this->getParent()->addChild(explotion);
    
    // Hide player
    setVisible(false);
    
    // Play sound effect if setting sound is ON
    if(CCUserDefault::sharedUserDefault()->getBoolForKey(SOUND_SETTING_KEY)) {
        CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("explotion01.wav");
    }
}

void Player::startFire() {
    
    // Still playing
    if(!isDead) {
        // Play sound effect if setting sound is ON
        if(CCUserDefault::sharedUserDefault()->getBoolForKey(SOUND_SETTING_KEY)) {
            CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("shoot.wav");
        }
     
        switch (bulletType) {
            case 1:
                fire(1);
                break;
                
            case 2:
                fire(2);
                break;
                
            case 3:
                fire(3);
                break;
                
            default:
                break;
        }
    }
}

void Player::fire(int type) {
    
    Bullet01 *bullet;
    
    if(type == 1) {
        bullet = Bullet01::create("bullet_01.png");
        bullet->damage = 1;
    }
    else if(type == 2) {
        bullet = Bullet01::create("bullet_02.png");
        bullet->damage = 2;
    } else {
        bullet = Bullet01::create("bullet_03.png");
        bullet->damage = 3;
    }
    
    // Fire bullet at head of player
    bullet->setPosition(ccp(getPosition().x, getPosition().y + getContentSize().height/2));
    
    // Move bullet to top of screen
    CCFiniteTimeAction *moveBullet = CCMoveTo::create(1.0, ccp(getPosition().x, size.height));
    
    // Call remove bullet after move done
    CCFiniteTimeAction *moveDone = CCCallFuncN::create(this, callfuncN_selector(Player::removeBullet));
    
    bullet->runAction(CCSequence::create(moveBullet, moveDone, NULL));
    
    BulletList->addObject(bullet);
    
    MainScene->addChild(bullet);
}

void Player::fireDouble() {
    
    Bullet01 *bullet01 = Bullet01::create("bullet_01.png");
    Bullet01 *bullet02 = Bullet01::create("bullet_01.png");
    
    // Fire bullet at head of player
    bullet01->setPosition(ccp(getPosition().x - 13, getPosition().y + 10));
    bullet02->setPosition(ccp(getPosition().x + 13, getPosition().y + 10));
    
    // Move bullet to top of screen
    CCFiniteTimeAction *moveBullet01 = CCMoveTo::create(1.0, ccp(getPosition().x - 10, size.height));
    CCFiniteTimeAction *moveBullet02 = CCMoveTo::create(1.0, ccp(getPosition().x + 10, size.height));
    
    // Call remove bullet after move done
    CCFiniteTimeAction *moveDone = CCCallFuncN::create(this, callfuncN_selector(Player::removeBullet));
    
    bullet01->runAction(CCSequence::create(moveBullet01, moveDone, NULL));
    bullet02->runAction(CCSequence::create(moveBullet02, moveDone, NULL));
    
    BulletList->addObject(bullet01);
    BulletList->addObject(bullet02);
    
    MainScene->addChild(bullet01);
    MainScene->addChild(bullet02);
}

void Player::fireTriple() {
    
//    fire();
//    fireDouble();
}

void Player::removeBullet(cocos2d::CCObject *sender) {
    
    CCSprite *bullet = (CCSprite*) sender;
    MainScene->removeChild(bullet, true);
    
    BulletList->removeObject(bullet);
}
